/**
 * 文件名：StateGridSearch.java
 *
 * 创建人：LS - ls121989@163.com
 *
 * 创建时间：2016年3月31日 下午6:51:30
 *
 * 版权所有：LS
 */
package fnic.prehand.esagent.ap_measure_new2;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.elasticsearch.search.aggregations.metrics.avg.Avg;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.aggregations.metrics.min.Min;

import fnic.prehand.common.AgentConfig;
import fnic.prehand.common.ReWriteJacksonFormat;
import fnic.prehand.domain.BandWithTypeResult;
import fnic.prehand.domain.DnsTypeResult;
import fnic.prehand.domain.FtpTypeResult;
import fnic.prehand.domain.HttpTypeResult;
import fnic.prehand.domain.Pop3TypeResult;
import fnic.prehand.domain.SmtpTypeResult;
import fnic.prehand.domain.TcpTypeResult;
import fnic.prehand.domain.TraceTypeResult;
import fnic.prehand.domain.UdpTypeResult;
import fnic.prehand.domain.XPingResult;
import fnic.prehand.esagent.EsAgent;

/**
 * [描述信息：说明类的基本功能]
 *
 * @author LS - ls121989@163.com
 * @version 1.0 Created on 2016年3月31日 下午6:51:30
 */
public class StateGridSearch extends EsAgent {

	// private static final Logger logger = Logger.getLogger(StateGridSearch.class);

	private static final ReWriteJacksonFormat jsonInstance = new ReWriteJacksonFormat();

	private static final AgentConfig config = AgentConfig.getInstance();

	private static final String myMuIndex = config.getMujob_IndexName();

	private static final String myMuType = config.getMujob_IndexType();

	private static final int maxSize = 10000;

	public StateGridSearch() {
	}

	public synchronized String performanceAnalysis(String start, String end, String target, String source, String type,
			String sourceIp) throws JsonGenerationException, IOException {
		QueryBuilder timeQuery = QueryBuilders.rangeQuery(Constant.TIMEFIELD).gte(start).lte(end);
		SearchRequestBuilder searchReq1 = client.prepareSearch(myMuIndex).setTypes(myMuType)
				.setSearchType(SearchType.QUERY_THEN_FETCH);
		SearchRequestBuilder searchReq;
		SearchResponse response;
		SearchHits hits;
		BoolQueryBuilder boolQuery;
		List<String> jsonres = new ArrayList<String>();

		switch (type) {
			case Constant.XPINGTYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.XPINGTOOL))
							.must(QueryBuilders.termQuery(Constant.XPINGTARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.XPINGTOOL))
							.must(QueryBuilders.termQuery(Constant.XPINGSOURCE, source));
					if (StringUtils.isNotBlank(sourceIp))
						boolQuery.must(QueryBuilders.termQuery(Constant.XPINGSOURCEIP, sourceIp));
				}
				searchReq = searchReq1
						.setQuery(boolQuery)
						.addFields(Constant.TIMEFIELD, Constant.RTTMIN, Constant.RTTAVG, Constant.RTTMAX,
								Constant.PACKETLOSS, Constant.RTTJITTER).setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();

				for (SearchHit hit : hits) {
					if (null == hit.field(Constant.TIMEFIELD) || null == hit.field(Constant.RTTMIN)
							|| null == hit.field(Constant.RTTAVG) || null == hit.field(Constant.RTTMAX)
							|| null == hit.field(Constant.PACKETLOSS) || null == hit.field(Constant.RTTJITTER))
						continue;
					// rewrite results
					XPingResult xps = new XPingResult((String) hit.field(Constant.TIMEFIELD).getValue(), null,
							String.valueOf(hit.field(Constant.RTTMIN).getValue()), String.valueOf(hit.field(
									Constant.RTTAVG).getValue()),
							String.valueOf(hit.field(Constant.RTTMAX).getValue()), String.valueOf(hit.field(
									Constant.PACKETLOSS).getValue()), String.valueOf(hit.field(Constant.RTTJITTER)
									.getValue()));
					jsonres.add(jsonInstance.Obj2String(xps));
				}
				break;

			case Constant.TRACETYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.TRACETOOL))
							.must(QueryBuilders.termQuery(Constant.TRACETARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.TRACETOOL))
							.must(QueryBuilders.termQuery(Constant.TRACESOURCE, source));
					if (StringUtils.isNotBlank(sourceIp))
						boolQuery.must(QueryBuilders.termQuery(Constant.TRACESOURCEIP, sourceIp));
				}
				searchReq = searchReq1.setQuery(boolQuery).setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();
				for (SearchHit hit : hits) {
					// 原先没有判断异常
					if (null == hit.getSource().get(Constant.TIMEFIELD)
							|| null == hit.getSource().get(Constant.TRACEROUTE))
						continue;
					TraceTypeResult tts = new TraceTypeResult((String) hit.getSource().get(Constant.TIMEFIELD), null,
							null, hit.getSource().get(Constant.TRACEROUTE));
					jsonres.add(jsonInstance.Obj2String(tts));
				}
				break;

			case Constant.BANDWIDTHTYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.BANDWIDTHTOOL))
							.must(QueryBuilders.termQuery(Constant.BANDWIDTHTARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.BANDWIDTHTOOL))
							.must(QueryBuilders.termQuery(Constant.BANDWIDTHSOURCE, source));
					if (StringUtils.isNotBlank(sourceIp))
						boolQuery.must(QueryBuilders.termQuery(Constant.BANDWIDTHSOURCEIP, sourceIp));
				}
				searchReq = searchReq1.setQuery(boolQuery)
						.addFields(Constant.TIMEFIELD, Constant.DOWNBANDWIDTH, Constant.UPBANDWIDTH).setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();
				for (SearchHit hit : hits) {
					if (null == hit.field(Constant.TIMEFIELD) || null == hit.field(Constant.DOWNBANDWIDTH)
							|| null == hit.field(Constant.UPBANDWIDTH)) {
						continue;
					}
					BandWithTypeResult bwts = new BandWithTypeResult((String) hit.field(Constant.TIMEFIELD).getValue(),
							null, String.valueOf(Double.parseDouble(hit.field(Constant.DOWNBANDWIDTH).getValue()
									.toString()) / 1000), String.valueOf(Double.parseDouble(hit
									.field(Constant.UPBANDWIDTH).getValue().toString()) / 1000));
					jsonres.add(jsonInstance.Obj2String(bwts));
				}
				break;

			case Constant.HTTPTYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.HTTPTOOL))
							.must(QueryBuilders.termQuery(Constant.HTTPTARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.HTTPTOOL))
							.must(QueryBuilders.termQuery(Constant.HTTPSOURCE, source));
					if (StringUtils.isNotBlank(sourceIp))
						boolQuery.must(QueryBuilders.termQuery(Constant.HTTPSOURCEIP, sourceIp));
				}
				searchReq = searchReq1
						.setQuery(boolQuery)
						.addFields(Constant.TIMEFIELD, Constant.DNSAVG, Constant.CONNECTAVG, Constant.FIRSTBYTEAVG,
								Constant.DOWNLOADAVG).setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();
				for (SearchHit hit : hits) {
					if (null == hit.field(Constant.TIMEFIELD) || null == hit.field(Constant.DNSAVG)
							|| null == hit.field(Constant.CONNECTAVG) || null == hit.field(Constant.FIRSTBYTEAVG)
							|| null == hit.field(Constant.DOWNLOADAVG)) {
						continue;
					}
					HttpTypeResult htr = new HttpTypeResult((String) hit.field(Constant.TIMEFIELD).getValue(), null,
							String.valueOf(hit.field(Constant.DNSAVG).getValue()), String.valueOf(hit.field(
									Constant.CONNECTAVG).getValue()), String.valueOf(hit.field(Constant.FIRSTBYTEAVG)
									.getValue()), String.valueOf(hit.field(Constant.DOWNLOADAVG).getValue()));
					jsonres.add(jsonInstance.Obj2String(htr));
				}
				break;

			case Constant.TCPTYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.TCPTOOL))
							.must(QueryBuilders.termQuery(Constant.TCPTARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.TCPTOOL))
							.must(QueryBuilders.termQuery(Constant.TCPSOURCE, source));
					if (StringUtils.isNotBlank(sourceIp))
						boolQuery.must(QueryBuilders.termQuery(Constant.TCPSOURCEIP, sourceIp));
				}
				searchReq = searchReq1
						.setQuery(boolQuery)
						.addFields(Constant.TIMEFIELD, Constant.CONNECTMINTIME, Constant.CONNECTAVGTIME,
								Constant.CONNECTMAXTIME).setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();
				for (SearchHit hit : hits) {
					if (null == hit.field(Constant.TIMEFIELD) || null == hit.field(Constant.CONNECTMINTIME)
							|| null == hit.field(Constant.CONNECTAVGTIME) || null == hit.field(Constant.CONNECTMAXTIME)) {
						continue;
					}
					int min = hit.field(Constant.CONNECTMINTIME).getValue();
					int avg = hit.field(Constant.CONNECTAVGTIME).getValue();
					int max = hit.field(Constant.CONNECTMAXTIME).getValue();
					TcpTypeResult ttr = new TcpTypeResult((String) hit.field(Constant.TIMEFIELD).getValue(), null,
							String.valueOf((double) min / 1000.0), String.valueOf((double) avg / 1000),
							String.valueOf((double) max / 1000));
					jsonres.add(jsonInstance.Obj2String(ttr));
				}
				break;

			case Constant.UDPTYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.UDPTOOL))
							.must(QueryBuilders.termQuery(Constant.UDPTARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.UDPTOOL))
							.must(QueryBuilders.termQuery(Constant.UDPSOURCE, source));
					if (StringUtils.isNotBlank(sourceIp))
						boolQuery.must(QueryBuilders.termQuery(Constant.UDPSOURCEIP, sourceIp));
				}
				searchReq = searchReq1
						.setQuery(boolQuery)
						.addFields(Constant.TIMEFIELD, Constant.DELAYMIN, Constant.DELAYAVG, Constant.DELAYMAX,
								Constant.JOGGLE, Constant.PACKETLOSS).setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();
				for (SearchHit hit : hits) {
					if (null == hit.field(Constant.TIMEFIELD) || null == hit.field(Constant.DELAYMIN)
							|| null == hit.field(Constant.DELAYAVG) || null == hit.field(Constant.DELAYMAX)
							|| null == hit.field(Constant.JOGGLE) || null == hit.field(Constant.PACKETLOSS)) {
						continue;
					}
					int min = hit.field(Constant.DELAYMIN).getValue();
					int avg = hit.field(Constant.DELAYAVG).getValue();
					int max = hit.field(Constant.DELAYMAX).getValue();
					double joggle = Double.parseDouble(String.valueOf(hit.field(Constant.JOGGLE).getValue()));
					UdpTypeResult utr = new UdpTypeResult((String) hit.field(Constant.TIMEFIELD).getValue(), null,
							String.valueOf((double) min / 1000), String.valueOf((double) avg / 1000),
							String.valueOf((double) max / 1000), String.valueOf(joggle / 1000), String.valueOf(hit
									.field(Constant.PACKETLOSS).getValue()));
					jsonres.add(jsonInstance.Obj2String(utr));
				}
				break;

			case Constant.SMTPTYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.SMTPTOOL))
							.must(QueryBuilders.termQuery(Constant.SMTPTARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.SMTPTOOL))
							.must(QueryBuilders.termQuery(Constant.SMTPSOURCE, source));
				}
				searchReq = searchReq1.setQuery(boolQuery)
						.addFields(Constant.TIMEFIELD, Constant.DNSTIME, Constant.CONNECTTIME, Constant.TOTALTIME)
						.setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();
				for (SearchHit hit : hits) {
					if (null == hit.field(Constant.TIMEFIELD) || null == hit.field(Constant.DNSTIME)
							|| null == hit.field(Constant.CONNECTTIME) || null == hit.field(Constant.TOTALTIME)) {
						continue;
					}
					int dns = hit.field(Constant.DNSTIME).getValue();
					int connect = hit.field(Constant.CONNECTTIME).getValue();
					int total = hit.field(Constant.TOTALTIME).getValue();
					SmtpTypeResult str = new SmtpTypeResult((String) hit.field(Constant.TIMEFIELD).getValue(), null,
							String.valueOf((double) dns / 1000), String.valueOf((double) connect / 1000),
							String.valueOf((double) total / 1000));
					jsonres.add(jsonInstance.Obj2String(str));
				}
				break;

			case Constant.POP3TYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.POP3TOOL))
							.must(QueryBuilders.termQuery(Constant.POP3TARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.POP3TOOL))
							.must(QueryBuilders.termQuery(Constant.POP3SOURCE, source));
				}
				searchReq = searchReq1
						.setQuery(boolQuery)
						.addFields(Constant.TIMEFIELD, Constant.DNSTIME, Constant.CONNECTTIME, Constant.LISTTIME,
								Constant.TOTALTIME).setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();
				for (SearchHit hit : hits) {
					if (null == hit.field(Constant.TIMEFIELD) || null == hit.field(Constant.DNSTIME)
							|| null == hit.field(Constant.CONNECTTIME) || null == hit.field(Constant.LISTTIME)
							|| null == hit.field(Constant.TOTALTIME)) {
						continue;
					}
					int dns = hit.field(Constant.DNSTIME).getValue();
					int connect = hit.field(Constant.CONNECTTIME).getValue();
					int list = hit.field(Constant.LISTTIME).getValue();
					int total = hit.field(Constant.TOTALTIME).getValue();
					Pop3TypeResult ptr = new Pop3TypeResult((String) hit.field(Constant.TIMEFIELD).getValue(), null,
							String.valueOf((double) dns / 1000), String.valueOf((double) connect / 1000),
							String.valueOf((double) list / 1000), String.valueOf((double) total / 1000));
					jsonres.add(jsonInstance.Obj2String(ptr));
				}
				break;

			case Constant.FTPTYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.FTPTOOL))
							.must(QueryBuilders.termQuery(Constant.FTPTARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.FTPTOOL))
							.must(QueryBuilders.termQuery(Constant.FTPSOURCE, source));
				}
				searchReq = searchReq1
						.setQuery(boolQuery)
						.addFields(Constant.TIMEFIELD, Constant.DNSTIME, Constant.CONNECTTIME, Constant.UPLOADTIME,
								Constant.DOWNLOADTIME, Constant.TOTALTIME).setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();
				for (SearchHit hit : hits) {
					if (null == hit.field(Constant.TIMEFIELD) || null == hit.field(Constant.DNSTIME)
							|| null == hit.field(Constant.CONNECTTIME) || null == hit.field(Constant.UPLOADTIME)
							|| null == hit.field(Constant.DOWNLOADTIME) || null == hit.field(Constant.TOTALTIME)) {
						continue;
					}
					int dns = hit.field(Constant.DNSTIME).getValue();
					int connect = hit.field(Constant.CONNECTTIME).getValue();
					int upload = hit.field(Constant.UPLOADTIME).getValue();
					int download = hit.field(Constant.DOWNLOADTIME).getValue();
					int total = hit.field(Constant.TOTALTIME).getValue();
					FtpTypeResult ftr = new FtpTypeResult((String) hit.field(Constant.TIMEFIELD).getValue(), null,
							String.valueOf((double) dns / 1000), String.valueOf((double) connect / 1000),
							String.valueOf((double) upload / 1000), String.valueOf((double) download / 1000),
							String.valueOf((double) total / 1000));
					jsonres.add(jsonInstance.Obj2String(ftr));
				}
				break;

			case Constant.DNSTYPE:
				if (StringUtils.isNotBlank(target)) {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.DNSTOOL))
							.must(QueryBuilders.termQuery(Constant.DNSTARGET, target));
				} else {
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.DNSTOOL))
							.must(QueryBuilders.termQuery(Constant.DNSSOURCE, source));
				}
				searchReq = searchReq1.setQuery(boolQuery)
						.addFields(Constant.TIMEFIELD, Constant.DNSMINTIME, Constant.DNSAVGTIME, Constant.DNSMAXTIME)
						.setSize(maxSize);
				response = searchReq.execute().actionGet();

				hits = response.getHits();
				for (SearchHit hit : hits) {
					if (null == hit.field(Constant.TIMEFIELD) || null == hit.field(Constant.DNSMINTIME)
							|| null == hit.field(Constant.DNSAVGTIME) || null == hit.field(Constant.DNSMAXTIME)) {
						continue;
					}
					int min = hit.field(Constant.DNSMINTIME).getValue();
					int avg = hit.field(Constant.DNSAVGTIME).getValue();
					int max = hit.field(Constant.DNSMAXTIME).getValue();
					DnsTypeResult dtr = new DnsTypeResult((String) hit.field(Constant.TIMEFIELD).getValue(), null,
							String.valueOf((double) min / 1000), String.valueOf((double) avg / 1000),
							String.valueOf((double) max / 1000));
					jsonres.add(jsonInstance.Obj2String(dtr));
				}
				break;
		}
		return jsonres.toString();
	}

	public synchronized String dataStatistic(String start, String end, String direction, String type)
			throws JsonGenerationException, IOException {
		QueryBuilder timeQuery = QueryBuilders.rangeQuery(Constant.TIMEFIELD).gte(start).lte(end);
		SearchRequestBuilder searchReq1 = client.prepareSearch(myMuIndex).setTypes(myMuType)
				.setSearchType(SearchType.QUERY_THEN_FETCH);
		SearchRequestBuilder searchReq;
		SearchResponse response;
		QueryBuilder boolQuery;
		Terms hostTerm;
		Iterator<Bucket> hostIter;
		List<String> jsonres = new ArrayList<String>();

		if (Constant.DESTINATIONDIRECTION.equals(direction)) {
			switch (type) {
				case Constant.XPINGTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.XPINGTOOL));
					searchReq = searchReq1.setQuery(boolQuery).addAggregation(
							AggregationBuilders.terms("host_term").field(Constant.XPINGTARGET)
									.subAggregation(AggregationBuilders.max("rtt_max").field(Constant.RTTMAX))
									.subAggregation(AggregationBuilders.min("rtt_min").field(Constant.RTTMIN))
									.subAggregation(AggregationBuilders.avg("rtt_avg").field(Constant.RTTAVG))
									.subAggregation(AggregationBuilders.avg("packet_loss").field(Constant.PACKETLOSS))
									.subAggregation(AggregationBuilders.avg("jitter").field(Constant.RTTJITTER)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Max maxRtt = bkt.getAggregations().get("rtt_max");
						Min minRtt = bkt.getAggregations().get("rtt_min");
						Avg avgRtt = bkt.getAggregations().get("rtt_avg");
						Avg avgPacketLoss = bkt.getAggregations().get("packet_loss");
						Avg avgJitter = bkt.getAggregations().get("jitter");
						XPingResult xps = new XPingResult(null, hostName, String.valueOf(minRtt.getValue()),
								String.valueOf(avgRtt.getValue()), String.valueOf(maxRtt.getValue()),
								String.valueOf(avgPacketLoss.getValue()), String.valueOf(avgJitter.getValue()));
						jsonres.add(jsonInstance.Obj2String(xps));
					}
					break;

				case Constant.TRACETYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.TRACETOOL));
					searchReq = searchReq1.setQuery(boolQuery).addAggregation(
							AggregationBuilders.terms("host_term").field(Constant.TRACETARGET)
									.subAggregation(AggregationBuilders.avg("neck").field(Constant.TRACENECK)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgNeck = bkt.getAggregations().get("neck");
						TraceTypeResult tts = new TraceTypeResult(null, hostName, String.valueOf(avgNeck.getValue()),
								null);
						jsonres.add(jsonInstance.Obj2String(tts));
					}
					break;

				case Constant.BANDWIDTHTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.BANDWIDTHTOOL));
					searchReq = searchReq1.setQuery(boolQuery).addAggregation(
							AggregationBuilders.terms("host_term").field(Constant.BANDWIDTHTARGET)
									.subAggregation(AggregationBuilders.avg("down_band").field(Constant.DOWNBANDWIDTH))
									.subAggregation(AggregationBuilders.avg("up_band").field(Constant.UPBANDWIDTH)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg downBand = bkt.getAggregations().get("down_band");
						Avg upBand = bkt.getAggregations().get("up_band");
						BandWithTypeResult bwts = new BandWithTypeResult(null, hostName, String.valueOf(downBand
								.getValue() / 1000), String.valueOf(upBand.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(bwts));
					}
					break;

				case Constant.HTTPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.HTTPTOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("host_term")
											.field(Constant.HTTPTARGET)
											.subAggregation(AggregationBuilders.avg("dns_avg").field(Constant.DNSAVG))
											.subAggregation(
													AggregationBuilders.avg("connect_avg").field(Constant.CONNECTAVG))
											.subAggregation(
													AggregationBuilders.avg("first_avg").field(Constant.FIRSTBYTEAVG))
											.subAggregation(
													AggregationBuilders.avg("download_avg").field(Constant.DOWNLOADAVG)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgDns = bkt.getAggregations().get("dns_avg");
						Avg avgConnect = bkt.getAggregations().get("connect_avg");
						Avg avgFirst = bkt.getAggregations().get("first_avg");
						Avg avgDownload = bkt.getAggregations().get("download_avg");
						HttpTypeResult htr = new HttpTypeResult(null, hostName, String.valueOf(avgDns.getValue()),
								String.valueOf(avgConnect.getValue()), String.valueOf(avgFirst.getValue()),
								String.valueOf(avgDownload.getValue()));
						jsonres.add(jsonInstance.Obj2String(htr));
					}
					break;

				case Constant.TCPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.TCPTOOL));
					searchReq = searchReq1.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("host")
											.field(Constant.TCPTARGET)
											.subAggregation(
													AggregationBuilders.min("connect_min_time").field(
															Constant.CONNECTMINTIME))
											.subAggregation(
													AggregationBuilders.avg("connect_avg_time").field(
															Constant.CONNECTAVGTIME))
											.subAggregation(
													AggregationBuilders.max("connect_max_time").field(
															Constant.CONNECTMAXTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Min minconnectmintime = bkt.getAggregations().get("connect_min_time");
						Avg avgconnectavgtime = bkt.getAggregations().get("connect_avg_time");
						Max maxconnectmaxtime = bkt.getAggregations().get("connect_max_time");
						TcpTypeResult ttr = new TcpTypeResult(null, hostName, String.valueOf(minconnectmintime
								.getValue() / 1000), String.valueOf(avgconnectavgtime.getValue() / 1000),
								String.valueOf(maxconnectmaxtime.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(ttr));
					}
					break;

				case Constant.UDPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.UDPTOOL));
					searchReq = searchReq1.setQuery(boolQuery).addAggregation(
							AggregationBuilders.terms("host").field(Constant.UDPTARGET)
									.subAggregation(AggregationBuilders.min("delay_min").field(Constant.DELAYMIN))
									.subAggregation(AggregationBuilders.avg("delay_avg").field(Constant.DELAYAVG))
									.subAggregation(AggregationBuilders.max("delay_max").field(Constant.DELAYMAX))
									.subAggregation(AggregationBuilders.avg("joggle").field(Constant.JOGGLE))
									.subAggregation(AggregationBuilders.avg("packet_loss").field(Constant.PACKETLOSS)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Min mindelaymin = bkt.getAggregations().get("delay_min");
						Avg avgdelayavg = bkt.getAggregations().get("delay_avg");
						Max maxdelaymax = bkt.getAggregations().get("delay_max");
						Avg avgjoggle = bkt.getAggregations().get("joggle");
						Avg avgpacketloss = bkt.getAggregations().get("packet_loss");
						UdpTypeResult utr = new UdpTypeResult(null, hostName,
								String.valueOf(mindelaymin.getValue() / 1000),
								String.valueOf(avgdelayavg.getValue() / 1000),
								String.valueOf(maxdelaymax.getValue() / 1000),
								String.valueOf(avgjoggle.getValue() / 1000),
								String.valueOf(avgpacketloss.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(utr));
					}
					break;

				case Constant.SMTPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.SMTPTOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("host")
											.field(Constant.SMTPTARGET)
											.subAggregation(AggregationBuilders.avg("dns_time").field(Constant.DNSTIME))
											.subAggregation(
													AggregationBuilders.avg("connect_time").field(Constant.CONNECTTIME))
											.subAggregation(
													AggregationBuilders.avg("total_time").field(Constant.TOTALTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgdnstime = bkt.getAggregations().get("dns_time");
						Avg avgconnecttime = bkt.getAggregations().get("connect_time");
						Avg avgtotal_time = bkt.getAggregations().get("total_time");
						SmtpTypeResult str = new SmtpTypeResult(null, hostName,
								String.valueOf(avgdnstime.getValue() / 1000),
								String.valueOf(avgconnecttime.getValue() / 1000), String.valueOf(avgtotal_time
										.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(str));
					}
					break;

				case Constant.POP3TYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.POP3TOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("host")
											.field(Constant.POP3TARGET)
											.subAggregation(AggregationBuilders.avg("dns_time").field(Constant.DNSTIME))
											.subAggregation(
													AggregationBuilders.avg("connect_time").field(Constant.CONNECTTIME))
											.subAggregation(
													AggregationBuilders.avg("list_time").field(Constant.LISTTIME))
											.subAggregation(
													AggregationBuilders.avg("total_time").field(Constant.TOTALTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgdnstime = bkt.getAggregations().get("dns_time");
						Avg avgconnecttime = bkt.getAggregations().get("connect_time");
						Avg avglisttime = bkt.getAggregations().get("list_time");
						Avg avgtotaltime = bkt.getAggregations().get("total_time");
						Pop3TypeResult ptr = new Pop3TypeResult(null, hostName,
								String.valueOf(avgdnstime.getValue() / 1000),
								String.valueOf(avgconnecttime.getValue() / 1000),
								String.valueOf(avglisttime.getValue() / 1000),
								String.valueOf(avgtotaltime.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(ptr));
					}
					break;

				case Constant.FTPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.FTPTOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("host")
											.field(Constant.FTPTARGET)
											.subAggregation(AggregationBuilders.avg("dns_time").field(Constant.DNSTIME))
											.subAggregation(
													AggregationBuilders.avg("connect_time").field(Constant.CONNECTTIME))
											.subAggregation(
													AggregationBuilders.avg("upload_time").field(Constant.UPLOADTIME))
											.subAggregation(
													AggregationBuilders.avg("download_time").field(
															Constant.DOWNLOADTIME))
											.subAggregation(
													AggregationBuilders.avg("total_time").field(Constant.TOTALTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgdnstime = bkt.getAggregations().get("dns_time");
						Avg avgconnecttime = bkt.getAggregations().get("connect_time");
						Avg avguploadtime = bkt.getAggregations().get("upload_time");
						Avg avgdownloadtime = bkt.getAggregations().get("download_time");
						Avg avgtotaltime = bkt.getAggregations().get("total_time");
						FtpTypeResult ftr = new FtpTypeResult(null, hostName,
								String.valueOf(avgdnstime.getValue() / 1000),
								String.valueOf(avgconnecttime.getValue() / 1000), String.valueOf(avguploadtime
										.getValue() / 1000), String.valueOf(avgdownloadtime.getValue() / 1000),
								String.valueOf(avgtotaltime.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(ftr));
					}
					break;

				case Constant.DNSTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.DNSTOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("host")
											.field(Constant.DNSTARGET)
											.subAggregation(
													AggregationBuilders.min("dns_min_time").field(Constant.DNSMINTIME))
											.subAggregation(
													AggregationBuilders.avg("dns_avg_time").field(Constant.DNSAVGTIME))
											.subAggregation(
													AggregationBuilders.max("dns_max_time").field(Constant.DNSMAXTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("host");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Min mindnsmintime = bkt.getAggregations().get("dns_min_time");
						Avg avgdnsavgtime = bkt.getAggregations().get("dns_avg_time");
						Max maxdnsmaxtime = bkt.getAggregations().get("dns_max_time");
						DnsTypeResult dtr = new DnsTypeResult(null, hostName,
								String.valueOf(mindnsmintime.getValue() / 1000), String.valueOf(avgdnsavgtime
										.getValue() / 1000), String.valueOf(maxdnsmaxtime.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(dtr));
					}
					break;
			}
		} else if (Constant.SOURCEDIRECTION.equals(direction)) {
			switch (type) {
				case Constant.XPINGTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.XPINGTOOL));
					searchReq = searchReq1.setQuery(boolQuery).addAggregation(
							AggregationBuilders.terms("probeid_term").field(Constant.XPINGSOURCE)
									.subAggregation(AggregationBuilders.max("rtt_max").field(Constant.RTTMAX))
									.subAggregation(AggregationBuilders.min("rtt_min").field(Constant.RTTMIN))
									.subAggregation(AggregationBuilders.avg("rtt_avg").field(Constant.RTTAVG))
									.subAggregation(AggregationBuilders.avg("packet_loss").field(Constant.PACKETLOSS))
									.subAggregation(AggregationBuilders.avg("jitter").field(Constant.RTTJITTER)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Max maxRtt = bkt.getAggregations().get("rtt_max");
						Min minRtt = bkt.getAggregations().get("rtt_min");
						Avg avgRtt = bkt.getAggregations().get("rtt_avg");
						Avg avgPacketLoss = bkt.getAggregations().get("packet_loss");
						Avg avgJitter = bkt.getAggregations().get("jitter");
						XPingResult xps = new XPingResult(null, hostName, String.valueOf(minRtt.getValue()),
								String.valueOf(avgRtt.getValue()), String.valueOf(maxRtt.getValue()),
								String.valueOf(avgPacketLoss.getValue()), String.valueOf(avgJitter.getValue()));
						jsonres.add(jsonInstance.Obj2String(xps));
					}
					break;

				case Constant.TRACETYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.TRACETOOL));
					searchReq = searchReq1.setQuery(boolQuery).addAggregation(
							AggregationBuilders.terms("probeid_term").field(Constant.TRACESOURCE)
									.subAggregation(AggregationBuilders.avg("neck").field(Constant.TRACENECK)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgNeck = bkt.getAggregations().get("neck");
						TraceTypeResult tts = new TraceTypeResult(null, hostName, String.valueOf(avgNeck.getValue()),
								null);
						jsonres.add(jsonInstance.Obj2String(tts));
					}
					break;

				case Constant.BANDWIDTHTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.BANDWIDTHTOOL));
					searchReq = searchReq1.setQuery(boolQuery).addAggregation(
							AggregationBuilders.terms("probeid_term").field(Constant.BANDWIDTHSOURCE)
									.subAggregation(AggregationBuilders.avg("down_band").field(Constant.DOWNBANDWIDTH))
									.subAggregation(AggregationBuilders.avg("up_band").field(Constant.UPBANDWIDTH)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg downBand = bkt.getAggregations().get("down_band");
						Avg upBand = bkt.getAggregations().get("up_band");
						BandWithTypeResult bwts = new BandWithTypeResult(null, hostName, String.valueOf(downBand
								.getValue() / 1000), String.valueOf(upBand.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(bwts));
					}
					break;

				case Constant.HTTPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.HTTPTOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("probeid_term")
											.field(Constant.HTTPSOURCE)
											.subAggregation(AggregationBuilders.avg("dns_avg").field(Constant.DNSAVG))
											.subAggregation(
													AggregationBuilders.avg("connect_avg").field(Constant.CONNECTAVG))
											.subAggregation(
													AggregationBuilders.avg("first_avg").field(Constant.FIRSTBYTEAVG))
											.subAggregation(
													AggregationBuilders.avg("download_avg").field(Constant.DOWNLOADAVG)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgDns = bkt.getAggregations().get("dns_avg");
						Avg avgConnect = bkt.getAggregations().get("connect_avg");
						Avg avgFirst = bkt.getAggregations().get("first_avg");
						Avg avgDownload = bkt.getAggregations().get("download_avg");
						HttpTypeResult htr = new HttpTypeResult(null, hostName, String.valueOf(avgDns.getValue()),
								String.valueOf(avgConnect.getValue()), String.valueOf(avgFirst.getValue()),
								String.valueOf(avgDownload.getValue()));
						jsonres.add(jsonInstance.Obj2String(htr));
					}
					break;

				case Constant.TCPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.TCPTOOL));
					searchReq = searchReq1.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("probeid_term")
											.field(Constant.TCPSOURCE)
											.subAggregation(
													AggregationBuilders.min("connect_min_time").field(
															Constant.CONNECTMINTIME))
											.subAggregation(
													AggregationBuilders.avg("connect_avg_time").field(
															Constant.CONNECTAVGTIME))
											.subAggregation(
													AggregationBuilders.max("connect_max_time").field(
															Constant.CONNECTMAXTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Min minconnectmintime = bkt.getAggregations().get("connect_min_time");
						Avg avgconnectavgtime = bkt.getAggregations().get("connect_avg_time");
						Max maxconnectmaxtime = bkt.getAggregations().get("connect_max_time");
						TcpTypeResult ttr = new TcpTypeResult(null, hostName, String.valueOf(minconnectmintime
								.getValue() / 1000), String.valueOf(avgconnectavgtime.getValue() / 1000),
								String.valueOf(maxconnectmaxtime.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(ttr));
					}
					break;

				case Constant.UDPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.UDPTOOL));
					searchReq = searchReq1.setQuery(boolQuery).addAggregation(
							AggregationBuilders.terms("probeid_term").field(Constant.UDPSOURCE)
									.subAggregation(AggregationBuilders.min("delay_min").field(Constant.DELAYMIN))
									.subAggregation(AggregationBuilders.avg("delay_avg").field(Constant.DELAYAVG))
									.subAggregation(AggregationBuilders.max("delay_max").field(Constant.DELAYMAX))
									.subAggregation(AggregationBuilders.avg("joggle").field(Constant.JOGGLE))
									.subAggregation(AggregationBuilders.avg("packet_loss").field(Constant.PACKETLOSS)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Min mindelaymin = bkt.getAggregations().get("delay_min");
						Avg avgdelayavg = bkt.getAggregations().get("delay_avg");
						Max maxdelaymax = bkt.getAggregations().get("delay_max");
						Avg avgjoggle = bkt.getAggregations().get("joggle");
						Avg avgpacketloss = bkt.getAggregations().get("packet_loss");
						UdpTypeResult utr = new UdpTypeResult(null, hostName,
								String.valueOf(mindelaymin.getValue() / 1000),
								String.valueOf(avgdelayavg.getValue() / 1000),
								String.valueOf(maxdelaymax.getValue() / 1000),
								String.valueOf(avgjoggle.getValue() / 1000),
								String.valueOf(avgpacketloss.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(utr));
					}
					break;

				case Constant.SMTPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.SMTPTOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("probeid_term")
											.field(Constant.SMTPSOURCE)
											.subAggregation(AggregationBuilders.avg("dns_time").field(Constant.DNSTIME))
											.subAggregation(
													AggregationBuilders.avg("connect_time").field(Constant.CONNECTTIME))
											.subAggregation(
													AggregationBuilders.avg("total_time").field(Constant.TOTALTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgdnstime = bkt.getAggregations().get("dns_time");
						Avg avgconnecttime = bkt.getAggregations().get("connect_time");
						Avg avgtotaltime = bkt.getAggregations().get("total_time");
						SmtpTypeResult str = new SmtpTypeResult(null, hostName,
								String.valueOf(avgdnstime.getValue() / 1000),
								String.valueOf(avgconnecttime.getValue() / 1000), String.valueOf(avgtotaltime
										.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(str));
					}
					break;

				case Constant.POP3TYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.POP3TOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("probeid_term")
											.field(Constant.POP3SOURCE)
											.subAggregation(AggregationBuilders.avg("dns_time").field(Constant.DNSTIME))
											.subAggregation(
													AggregationBuilders.avg("connect_time").field(Constant.CONNECTTIME))
											.subAggregation(
													AggregationBuilders.avg("list_time").field(Constant.LISTTIME))
											.subAggregation(
													AggregationBuilders.avg("total_time").field(Constant.TOTALTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgdnstime = bkt.getAggregations().get("dns_time");
						Avg avgconnecttime = bkt.getAggregations().get("connect_time");
						Avg avglisttime = bkt.getAggregations().get("list_time");
						Avg avgtotaltime = bkt.getAggregations().get("total_time");
						Pop3TypeResult ptr = new Pop3TypeResult(null, hostName,
								String.valueOf(avgdnstime.getValue() / 1000),
								String.valueOf(avgconnecttime.getValue() / 1000),
								String.valueOf(avglisttime.getValue() / 1000),
								String.valueOf(avgtotaltime.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(ptr));
					}
					break;

				case Constant.FTPTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.FTPTOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("probeid_term")
											.field(Constant.FTPSOURCE)
											.subAggregation(AggregationBuilders.avg("dns_time").field(Constant.DNSTIME))
											.subAggregation(
													AggregationBuilders.avg("connect_time").field(Constant.CONNECTTIME))
											.subAggregation(
													AggregationBuilders.avg("upload_time").field(Constant.UPLOADTIME))
											.subAggregation(
													AggregationBuilders.avg("download_time").field(
															Constant.DOWNLOADTIME))
											.subAggregation(
													AggregationBuilders.avg("total_time").field(Constant.TOTALTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Avg avgdnstime = bkt.getAggregations().get("dns_time");
						Avg avgconnecttime = bkt.getAggregations().get("connect_time");
						Avg avguploadtime = bkt.getAggregations().get("upload_time");
						Avg avgdownloadtime = bkt.getAggregations().get("download_time");
						Avg avgtotaltime = bkt.getAggregations().get("total_time");
						FtpTypeResult ftr = new FtpTypeResult(null, hostName,
								String.valueOf(avgdnstime.getValue() / 1000),
								String.valueOf(avgconnecttime.getValue() / 1000), String.valueOf(avguploadtime
										.getValue() / 1000), String.valueOf(avgdownloadtime.getValue() / 1000),
								String.valueOf(avgtotaltime.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(ftr));
					}
					break;

				case Constant.DNSTYPE:
					boolQuery = QueryBuilders.boolQuery().must(timeQuery)
							.must(QueryBuilders.termQuery(Constant.TOOLFIELD, Constant.DNSTOOL));
					searchReq = searchReq1
							.setQuery(boolQuery)
							.addAggregation(
									AggregationBuilders
											.terms("probeid_term")
											.field(Constant.DNSSOURCE)
											.subAggregation(
													AggregationBuilders.min("dns_min_time").field(Constant.DNSMINTIME))
											.subAggregation(
													AggregationBuilders.avg("dns_avg_time").field(Constant.DNSAVGTIME))
											.subAggregation(
													AggregationBuilders.max("dns_max_time").field(Constant.DNSMAXTIME)));
					response = searchReq.execute().actionGet();

					hostTerm = response.getAggregations().get("probeid_term");
					hostIter = hostTerm.getBuckets().iterator();
					while (hostIter.hasNext()) {
						Bucket bkt = hostIter.next();
						String hostName = bkt.getKey();
						Min mindnsmintime = bkt.getAggregations().get("dns_min_time");
						Avg avgdnsavgtime = bkt.getAggregations().get("dns_avg_time");
						Max maxdnsmaxtime = bkt.getAggregations().get("dns_max_time");
						DnsTypeResult dtr = new DnsTypeResult(null, hostName,
								String.valueOf(mindnsmintime.getValue() / 1000), String.valueOf(avgdnsavgtime
										.getValue() / 1000), String.valueOf(maxdnsmaxtime.getValue() / 1000));
						jsonres.add(jsonInstance.Obj2String(dtr));
					}
					break;
			}
		}
		return jsonres.toString();
	}

	public static void main(String[] args) throws JsonGenerationException, IOException {
		// StateGridSearch pm = new StateGridSearch();
		// pm.dataStatistic("2016-05-01 00:00:00", "2016-09-01 00:00:00", "2", "1");
		int a = 456;
		System.out.println((double) a / 1000);
	}
}
